package com.zqxq.lottery.shedule;

import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.service.sys.ISysActivityService;
import com.zqxq.lottery.service.sys.ISysCouponService;
import com.zqxq.lottery.service.sys.ISysMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 苦智
 * @date 2018/5/26 11:48
 * @description 用户活动定时任务
 * @ClassName CouponJob
 */
@Component
@Slf4j
public class ActivityJob {
    @Autowired
    private YmlConfig ymlConfig;
    @Autowired
    private ISysCouponService sysCouponService;
    @Autowired
    private ISysActivityService sysActivityService;
    @Autowired
    private ISysMsgService sysMsgService;

    private Boolean canRun(){
        return "true".equals(ymlConfig.getActivitySchedule());
    }

    @Scheduled(cron = "1 0 0 * * ?")
    public void executeOverTimeCoupon(){
        if (canRun()){
            log.info("自动任务--用户优惠券过期计算开始------------------");
            try {
                sysCouponService.updateUserCoupon();
            } catch (Exception e){
                log.error("自动任务--用户优惠券过期计算失败",e);
            }
        }
    }
    @Scheduled(cron = "1 0 0 * * ?")
    public void executeAcceptingCoupon(){
        if (canRun()){
            log.info("自动任务--领取待领奖优惠券------------------");
            try {
                sysActivityService.acceptingCoupon();
            } catch (Exception e){
                log.error("自动任务--领取待领奖优惠券",e);
            }
        }
    }
    @Scheduled(cron = "0 59 23 1/2 * ? ") //每天23:59:00执行
    public void executeUpdateMsg(){
        if (canRun()){
            log.info("自动任务--定时更新首页大奖通知消息------------------");
            try {
                sysMsgService.updateMsg();
            } catch (Exception e){
                log.error("自动任务--定时更新首页大奖通知消息失败",e);
            }
        }
    }

}
